package com.ccsu.eb.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccsu.eb.model.EbProduct;
import com.ccsu.eb.model.EbProductCategory;
import com.ccsu.eb.service.EbProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2022/3/18 08:16
 */
@Controller
@RequestMapping("/product")
public class EbProductController {

    @Resource
    private EbProductService ebProductService;

    /**
     * 四大域对象：pageContext request(只在一次请求范围有效) session(一次会话范围有效) application(整个应用范围有效)
     * @param model
     * @param pid
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Model model,Integer pid){//alt+enter
        System.out.println("进入EbProductController的detail方法，pid=="+pid);
        //TODO 1.调用service根据ID获取商品 和 当前点击的商品的分类信息
        EbProduct productDetail = ebProductService.detail(pid);
        Map<String, EbProductCategory> resultCateMap = ebProductService.linkedCateByProduc(pid);
        //TODO 2.绑定到model---数据
        model.addAttribute("detail",productDetail);
        model.addAttribute("cateMap",resultCateMap);
        //TODO  3.返回view--html
        return "product-view";//templates/product-view.html
    }

    /**
     * 根据关键字搜索商品列表
     * @param keyword   关键字
     * @param pnum      页数
     * @return
     */
    @RequestMapping("/search")
    public String search(Model model,String keyword, @RequestParam(value = "pnum",defaultValue = "1") Integer pnum){
        System.out.println("进入EbProductController的search方法，keyword:"+keyword);
        //TODO 1.调用service层根据关键字搜索商品
        Page productListPage= ebProductService.search(keyword,pnum);
        //TODO 2.绑定数据到model
        model.addAttribute("productPage",productListPage);
        //绑定搜索的关键字---回显
        model.addAttribute("keyword",keyword);
        //TODO  3.返回view--html
        return "product-list";//templates/product-list.html
    }
}
